package com.baofu.paycore.service.factory.impl;

import com.baofu.paycore.common.utils.Constant;
import com.baofu.paycore.manager.model.preAuth.PreAuthBO;
import com.baofu.paycore.service.facade.model.PayBaseDTO;
import com.baofu.paycore.service.facade.model.authconfirm.AuthConfirmDTO;
import com.baofu.paycore.service.factory.OrderFactory;
import com.baofu.paycore.service.factory.PayBaseFactory;
import com.baofu.paycore.service.processor.PayBaseProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 预授权完成单 组装工厂类
 *
 * <p>
 *  1.组装预授权完成模型
 * </p>
 * User: xin deng  Date: 16/8/31 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Service
public class PreAuthConfirmFactoryImpl extends PayBaseFactory implements OrderFactory {

    /**
     * 主单处理器
     */
    @Resource(name = "preAuthConfirmProcessor")
    private PayBaseProcessor commonProcessor;

    /**
     * 资金单处理器
     */
    @Resource(name = "subPreAuthConfirmProcessor")
    private PayBaseProcessor fundProcessor;

    @PostConstruct
    public void init(){
        processor.add(commonProcessor);
        processor.add(fundProcessor);
    }

    public PreAuthBO build(AuthConfirmDTO baseDTO,PreAuthBO preAuthBO){
        PreAuthBO authBO = this.build(baseDTO);
        authBO.setPayerNo(preAuthBO.getPayerNo());
        authBO.setAuthAmt(preAuthBO.getAmt() - Constant.validateLong(preAuthBO.getDealAmt()));
        return authBO;
    }

    /**
     * 组装支付模型
     *
     * @param baseDTO 请求对象
     * @return 支付模型
     */
    @Override
    public PreAuthBO build(PayBaseDTO baseDTO) {
        PreAuthBO preAuthBO = new PreAuthBO();
        super.buildBO(preAuthBO, baseDTO);
        log.debug("预授权完成组装参数:{}", preAuthBO);
        return preAuthBO;
    }
}
